//
//  NYMyVipBuyCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/9.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYMyVipBuyCell.h"
@interface NYMyVipBuyCell ()
@property(nonatomic, strong) TTTAttributedLabel *titleLabel;
@property(nonatomic, strong) TTTAttributedLabel *subTitleLabel;
@property(nonatomic, strong) QMUIGhostButton *actionButton;

@end
@implementation NYMyVipBuyCell
- (BOOL)shouldUpdateCellWithObject:(id)object
{
        self.vipPayModel = object;
        return YES;
}
- (void)setVipPayModel:(NYVipPayModel *)vipPayModel
{
        _vipPayModel = vipPayModel;
        [self updateContent];
}
- (void)updateContent
{
        NSDictionary *norAttr = @{
                                  NSForegroundColorAttributeName : UIColorHex(0x333333),
                                  NSFontAttributeName : WGSystem16Font,
                                  };
        NSDictionary *selAttr = @{
                                  NSForegroundColorAttributeName : UIColorHex(0xFFA336),
                                  NSFontAttributeName : WGSystem16Font,
                                  };
        NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:NOT_NULL_STRING_DEFAULT_EMPTY(self.vipPayModel.name) attributes:norAttr];
        [attrM appendAttributedString:[[NSAttributedString alloc] initWithString:NOT_NULL_STRING_DEFAULT_EMPTY(self.vipPayModel.price) attributes:selAttr]];
        self.titleLabel.attributedText = attrM;
        self.subTitleLabel.text = self.vipPayModel.discount;
        NSString *title = self.vipPayModel.isBuyed ? @"恢复" : @"开通";
        [self.actionButton setTitle:title forState:(UIControlStateNormal)];
}
- (void)setupView
{
        [super setupView];
        UIView *bottomView = [[UIView alloc] init];
        [self.contentView addSubview:bottomView];
        [bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.edges.equalTo(self.contentView).valueOffset([NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(__WGWidth(17), __WGWidth(15), __WGWidth(0), __WGWidth(0))]);
        }];
        bottomView.qmui_borderPosition = QMUIBorderViewPositionBottom;
        [bottomView addSubview:self.titleLabel];
        [bottomView addSubview:self.subTitleLabel];
        [bottomView addSubview:self.actionButton];
        
//        [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.left.top.equalTo(bottomView);
//        }];
//
//        [self.subTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//                make.top.equalTo(self.titleLabel.mas_bottom).offset(__WGHeight(8));
//                make.left.equalTo(self.titleLabel);
//                make.bottom.equalTo(bottomView.mas_bottom).offset(-__WGHeight(16));;
//        }];
        [self.actionButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self.contentView);
                make.right.equalTo(bottomView.mas_right).offset(-__WGWidth(16));
                make.height.mas_equalTo(__WGHeight(28));
                make.width.mas_equalTo(__WGWidth(62));
        }];
        
        UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.titleLabel,self.subTitleLabel]];
        stackView.axis = UILayoutConstraintAxisVertical;
        stackView.distribution = UIStackViewDistributionFillProportionally;
        stackView.alignment = UIStackViewAlignmentFill;
        stackView.spacing = 8;
        [bottomView addSubview:stackView];
        [stackView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.actionButton.mas_left).offset(-5);
                make.left.top.equalTo(bottomView);
                make.bottom.equalTo(bottomView.mas_bottom).offset(-__WGHeight(16));
        }];

        @weakify(self);
        [self.actionButton jk_addActionHandler:^(NSInteger tag) {
                @strongify(self);
                if (self.actionBlock) {
                        self.actionBlock(self.vipPayModel);
                }
        }];

}


- (TTTAttributedLabel *)titleLabel
{
        if (_titleLabel == nil) {
                _titleLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        }
        return _titleLabel;
}
- (TTTAttributedLabel *)subTitleLabel
{
        if (_subTitleLabel == nil) {
                _subTitleLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
        }
        return _subTitleLabel;
}
- (QMUIGhostButton *)actionButton
{
        if (_actionButton == nil) {
                _actionButton = [[QMUIGhostButton alloc] init];
                _actionButton.ghostColor = UIColorHex(0x836131);
                _actionButton.borderWidth = 1;
                _actionButton.cornerRadius = 14;
                _actionButton.clipsToBounds = YES;
                [_actionButton setTitle:@"开通" forState:(UIControlStateNormal)];
                _actionButton.backgroundColor = [UIColor jk_gradientFromColor:UIColorHex(0xD0B27C) toColor:UIColorHex(0xEED9AE) withHeight:62];
        }
        return _actionButton;
}
@end
